package com.day08.homework.dao;

import com.day08.homework.vo.Book;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements IBookDao {

    private Connection connection = null;
    private PreparedStatement preparedStatement = null;

    public BookDaoImpl(Connection _connection) {
        this.connection = _connection;
    }

    @Override
    public List<Book> selectBookInfoAll() throws Exception {
        List<Book> bookList = new ArrayList<>();
        String sql = "SELECT book_id, book_isbn, book_name, book_price, book_publishTime FROM book;";
        this.preparedStatement = this.connection.prepareStatement(sql);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        Book book = null;
        while (resultSet.next()) {
            book = new Book();
            book.setBookId(resultSet.getInt(1));
            book.setBookIsbn(resultSet.getString(2));
            book.setBookName(resultSet.getString(3));
            book.setBookPrice(resultSet.getInt(4));
            book.setBookPublishTime(resultSet.getDate(5));
            bookList.add(book);
        }
        return bookList;
    }

    @Override
    public List<Book> selectBookInfoOne(Integer bookId) throws Exception {
        List<Book> bookList = new ArrayList<>();
        String sql = "SELECT book_id, book_isbn, book_name, book_price, book_publishTime FROM book WHERE book_id = ?;";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setInt(1, bookId);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        Book book = null;
        while (resultSet.next()) {
            book = new Book();
            book.setBookId(resultSet.getInt(1));
            book.setBookIsbn(resultSet.getString(2));
            book.setBookName(resultSet.getString(3));
            book.setBookPrice(resultSet.getInt(4));
            book.setBookPublishTime(resultSet.getDate(5));
            bookList.add(book);
        }
        return bookList;
    }

    @Override
    public List<Book> selectBookInfoLike(String bookName) throws Exception {
        List<Book> bookList = new ArrayList<>();
        String sql = "SELECT book_id, book_isbn, book_name, book_price, book_publishTime FROM book WHERE book_name like ?;";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, "%" + bookName + "%");
        ResultSet resultSet = this.preparedStatement.executeQuery();
        Book book = null;
        while (resultSet.next()) {
            book = new Book();
            book.setBookId(resultSet.getInt(1));
            book.setBookIsbn(resultSet.getString(2));
            book.setBookName(resultSet.getString(3));
            book.setBookPrice(resultSet.getInt(4));
            book.setBookPublishTime(resultSet.getDate(5));
            bookList.add(book);
        }
        return bookList;
    }

    @Override
    public boolean insertBookInfo(Book book) throws Exception {
        boolean flag = false;
        String sql = "INSERT INTO book(book_id, book_isbn, book_name, book_price, book_publishTime) VALUES (?, ?, ?, ?, ?);";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setInt(1, book.getBookId());
        this.preparedStatement.setString(2, book.getBookIsbn());
        this.preparedStatement.setString(3, book.getBookName());
        this.preparedStatement.setInt(4, book.getBookPrice());
        this.preparedStatement.setDate(5, (Date) book.getBookPublishTime());
        if (this.preparedStatement.executeUpdate() > 0) {
            flag = true;
        }
        this.preparedStatement.close();
        return flag;
    }

    @Override
    public boolean deleteBookInfo(Integer bookId) throws Exception {
        boolean flag = false;
        String sql = "DELETE FROM book WHERE book_id = ?;";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setInt(1, bookId);
        if (this.preparedStatement.executeUpdate() > 0) {
            flag = true;
        }
        this.preparedStatement.close();
        return flag;
    }

    @Override
    public boolean updateBookInfo(Integer bookId, Book book) throws Exception {
        boolean flag = false;
        String sql = "UPDATE book set book_id=?, book_isbn=?, book_name=?, book_price=?, book_publishTime=? WHERE book_id = ?;";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setInt(1, book.getBookId());
        this.preparedStatement.setString(2, book.getBookIsbn());
        this.preparedStatement.setString(3, book.getBookName());
        this.preparedStatement.setInt(4, book.getBookPrice());
        this.preparedStatement.setDate(5, (Date) book.getBookPublishTime());
        this.preparedStatement.setInt(6, bookId);
        if (this.preparedStatement.executeUpdate() > 0) {
            flag = true;
        }
        this.preparedStatement.close();
        return flag;
    }
}
